#include <Wire.h>
//#include <inttypes.h>

#include <WiFi.h>
#include <WiFiUdp.h>

// WiFi network name and password:
const char * networkName = "hardware_2.4g";
const char * networkPswd = "ToBeNO.0";

//IP address to send UDP data to:
// either use the ip address of the server or 
// a network broadcast address
const char * udpAddress = "192.168.9.159";
const int udpPort = 3333;

//Are we currently connected?
boolean connected = false;

//The udp library class
WiFiUDP udp;

void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  Serial.begin(115200);

  Wire.beginTransmission(72);
  Wire.write(byte(0x4c));
  Wire.endTransmission();

    //Connect to the WiFi network
  connectToWiFi(networkName, networkPswd);
}

int i = 0x00;
int8_t valu[2];

int a = 0;

int item = 0;

float fmv; 
int16_t mv = 0;


void loop() {
  // put your main code here, to run repeatedly:
  //only send data when connected
//  if(connected){
//    //Send a packet
//    udp.beginPacket(udpAddress,udpPort);
//    udp.printf("Seconds since boot: %lu", millis()/1000);
//    udp.endPacket();
//  }  
  item = item+1;
  udp.beginPacket(udpAddress,udpPort);
  udp.printf("Item %d",item);
  udp.endPacket();
  Serial.printf("Item %d\r\n",item);


//通道0 采集
  Wire.beginTransmission(72);
  Wire.write(byte(0x4c));
  Wire.endTransmission();
  i = 0;
  byte buf0[2]; 
  buf0 = {0x00,0x00,0x00};
  delay(500);
      

//  Serial.println("helloWorld");
//  Serial.println(Wire.available());
  a=0;
  Wire.requestFrom(72,3);
  while (Wire.available()) {
    buf0[a] = Wire.read();
    Serial.print(buf0[a],HEX);

    a = a+1;
  }
  if(!bitRead(buf0[2],7)){
    mv = buf0[0]<<8;
    mv = mv + buf0[1];
    fmv = mv*2048.00/32768;
    Serial.printf(" ");
    Serial.printf("chanel %d = %f mV",i,fmv);
    Serial.println();

    udp.beginPacket(udpAddress,udpPort);
    udp.printf("chanel %d = %f mV",i,fmv);
    udp.endPacket();
  }else{
    Serial.printf(" ");
    Serial.printf("chanel %d = null\r\n",i);
  }

 
//通道1 采集
   Wire.beginTransmission(72);
   Wire.write(byte(0x6c));
   Wire.endTransmission();
   i = 1;
   byte buf1[2];
   buf1 = {0x00,0x00,0x00};
   delay(500);
  

//  Serial.println("helloWorld");
//  Serial.println(Wire.available());
  a=0;
  Wire.requestFrom(72,3);
  while (Wire.available()) {
    buf1[a] = Wire.read();
    Serial.print(buf1[a],HEX);
    a = a+1;
  }
//  Serial.print(!bitRead(buf1[2],7),HEX);
  if(!bitRead(buf1[2],7)){
    mv = buf1[0]<<8;
    mv = mv + buf1[1];
    fmv = mv*2048.00/32768;
    Serial.printf(" ");
    Serial.printf("chanel %d = %f mV",i,fmv);
    Serial.println();

    udp.beginPacket(udpAddress,udpPort);
    udp.printf("chanel %d = %f mV",i,fmv);
    udp.endPacket();
  }else{
    Serial.printf(" ");
    Serial.printf("chanel %d = null\r\n",i);
  }


  delay(1000);
}



void connectToWiFi(const char * ssid, const char * pwd){
  Serial.println("Connecting to WiFi network: " + String(ssid));

  // delete old config
  WiFi.disconnect(true);
  //register event handler
  WiFi.onEvent(WiFiEvent);
  
  //Initiate connection
  WiFi.begin(ssid, pwd);

  Serial.println("Waiting for WIFI connection...");
}

//wifi event handler
void WiFiEvent(WiFiEvent_t event){
    switch(event) {
      case SYSTEM_EVENT_STA_GOT_IP:
          //When connected set 
          Serial.print("WiFi connected! IP address: ");
          Serial.println(WiFi.localIP());  
          //initializes the UDP state
          //This initializes the transfer buffer
          udp.begin(WiFi.localIP(),udpPort);
          connected = true;
          break;
      case SYSTEM_EVENT_STA_DISCONNECTED:
          Serial.println("WiFi lost connection");
          connected = false;
          break;
      default: break;
    }
}
